package com.photoeditor.demo.model.face;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.common.base.ComponentContext;
import com.photoeditor.R;
import com.photoeditor.demo.model.bean.EthnicityBean;
import com.photoeditor.demo.model.bean.EthnicityDataManager;
import com.photoeditor.demo.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import static com.photoeditor.demo.model.face.EthnicitySelectAdapter.OTHER_VALUE;

/**
 * Created by @author winn on 2018/10/30.
 */

public class EthnicitySelectDialog extends DialogFragment implements BaseQuickAdapter.OnItemChildClickListener {

    private EthnicitySelectAdapter mAdapter;

    @Override
    public Dialog onCreateDialog(Bundle bundle) {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.dialog_ethnicity_select, null, false);
        Dialog create = new Builder(getContext()).setView(inflate).create();
        initView(inflate);
        return create;
    }

    private void initView(View view) {
        RecyclerView recyclerView = view.findViewById(R.id.listview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        initListView(recyclerView);
        initData();
        recyclerView.setAdapter(mAdapter);

        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initData() {
        mAdapter = new EthnicitySelectAdapter();
        List<EthnicityBean> data = EthnicityDataManager.getInstance().getEthnicity(null);
        mAdapter.addData(data);
        mAdapter.setOnItemChildClickListener(this);
    }

    public void initListView(RecyclerView recyclerView) {
        recyclerView.getItemAnimator().setAddDuration(0);
        recyclerView.getItemAnimator().setChangeDuration(0);
        recyclerView.getItemAnimator().setMoveDuration(0);
        recyclerView.getItemAnimator().setRemoveDuration(0);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        EthnicityBean bean = mAdapter.getItem(position);
        if (bean.getItemType() == EthnicityBean.ETHNICITY_TYPE_PARENT && bean.isHasChild()) {
            expand(bean);
        } else {
            if (OTHER_VALUE.equalsIgnoreCase(bean.getItemName())){
                ToastUtil.showToast(ComponentContext.getContext().getString(R.string.baby_select_other_tips));
            } else {
                selItem(bean);
            }
        }
    }

    private void expand(EthnicityBean bean) {
        // 展开或收起子项
        bean.setExpand(!bean.isExpand());
        List<EthnicityBean> data = EthnicityDataManager.getInstance().getEthnicity(mAdapter.getData());
        mAdapter.replaceData(data);
    }

    private void selItem(EthnicityBean bean) {
        EventBus.getDefault().post(bean);

        initStatistics(bean.getItemName());
        // 选中
        dismissAllowingStateLoss();
    }

    private void initStatistics(String itemName) {
        String type = null;
//        if (itemName.equals("Caucasian")){
//            type = StatisticsConstant.STATISTIC_EVENT_VALUE_BABY_SELECT_CAUCASION;
//        } else if (itemName.equals("Middle Eastern/North African")){
//            type = StatisticsConstant.STATISTIC_EVENT_VALUE_BABY_SELECT_MIDDLE_EASTERN;
//        }if (itemName.equals("East Asian")){
//            type = StatisticsConstant.STATISTIC_EVENT_VALUE_BABY_SELECT_EAST_ASIAN;
//        }if (itemName.equals("Southeast Asian")){
//            type = StatisticsConstant.STATISTIC_EVENT_VALUE_BABY_SELECT_SOUTHEAST_ASIAN;
//        }if (itemName.equals("South Asian")){
//            type = StatisticsConstant.STATISTIC_EVENT_VALUE_BABY_SELECT_SOUTH_ASIAN;
//        }if (itemName.equals("Hispanic")){
//            type = StatisticsConstant.STATISTIC_EVENT_VALUE_BABY_SELECT_HISPANIC;
//        }if (itemName.equals("Latino")){
//            type = StatisticsConstant.STATISTIC_EVENT_VALUE_BABY_SELECT_LATINO;
//        }if (itemName.equals("Black")){
//            type = StatisticsConstant.STATISTIC_EVENT_VALUE_BABY_SELECT_BLACK;
//        }
//
//        StatisticsUtils.statisticsCustomFunction(StatisticsConstant.STATISTIC_EVENT_FUN_FEATURE
//                , StatisticsConstant.STATISTIC_EVENT_KEY_BABY,
//                StatisticsConstant.STATISTIC_EVENT_VALUE_BABY_SELECT_BUTTON
//                        + StatisticsConstant.STATISTIC_EVENT_VALUE_CLICK + type);
    }

}
